package wat.serwerapp.main;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import wat.serwerapp.usermgmt.ldap.MethodManager;

/**
 *
 * @author P.B. & L.G. & K.K.
 */
public class Parameters {

    private static final String REPOSITORY_DIR = "erp.ws.login.repository.dir";
    private static final String LDAP_CONNECTION = "erp.ws.ldap";
    private static final String USE_SSL = "USE_SSL";
    private static final String SSL_CERT_FILE = "SSL_CERT_FILE";
    private static final String SSL_CERT_PASSWORD = "SSL_CERT_PASSWORD";
    private static Parameters instance;
    private Properties properties;

    private Parameters() {
        try {
            //File file = new File("erp-server.properties");
            properties = new Properties();
            InputStream is = getClass().getResourceAsStream("/erp-server.properties");
            properties.load(is);
            //if(file.exists()) {
            System.out.println("Poprawnie zaladowano plik konfiguracyjny: erp-server.properties");
            //}
        } catch (IOException ioe) {
            Logger.getLogger(Parameters.class.getName()).log(Level.SEVERE, "Blad podczas otwierania pliku erp-server.properties - zostana uzyte ustawienia domyslne", ioe);
            properties.setProperty(REPOSITORY_DIR, "c:/repo/");
            properties.setProperty(LDAP_CONNECTION, "ldap://localhost:389/");
            properties.setProperty(SSL_CERT_FILE, "/erpopen.cert");
            properties.setProperty(SSL_CERT_PASSWORD, "erpopenpw");
        }
    }

    public static Parameters getInstance() {
        if (instance == null) {
            instance = new Parameters();
        }
        return instance;
    }

    public String getRepositoryDir() {
        return properties.getProperty(REPOSITORY_DIR);
    }

    public boolean isUseSSL() {
        return new Boolean(properties.getProperty(USE_SSL));

    }

    public String getSslCertFile() {
        return properties.getProperty(SSL_CERT_FILE);
    }

    public String getSslCertPassword() {
        return properties.getProperty(SSL_CERT_PASSWORD);
    }
     public String getLDAPConnection() {
        return properties.getProperty(LDAP_CONNECTION);
    }
    // !!!!!!!!! do zmiany - z xml'a ???? !!!!!!!!!!!!!!!!!!!!!!!
    public Map<String, String> getAvailableMethods() {
        try {
//        HashMap<String,String> methods = new HashMap<String, String>();
//
//        methods.put("getUsersList", "pl.edu.wat.erp.methods.GetUsersList");
//        methods.put("getRolesList", "pl.edu.wat.erp.methods.GetRolesList");
//        methods.put("addRole", "pl.edu.wat.erp.methods.AddRole");
//        methods.put("addUser", "pl.edu.wat.erp.methods.AddUser");
//        methods.put("removeRole", "pl.edu.wat.erp.methods.RemoveRole");
//        methods.put("removeUser", "pl.edu.wat.erp.methods.RemoveUser");
//        methods.put("setRolesList", "pl.edu.wat.erp.methods.SetRolesList");
//        methods.put("setUsersList", "pl.edu.wat.erp.methods.SetUsersList");
//
//        return methods;
            return MethodManager.getMethods();
        } catch (NamingException ex) {
            Logger.getLogger(Parameters.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }
}
